/*
    Create Date : 
    Author    : Nguyen Duy Tan
    Copyright(c) 2009 A.D.A Solution. All rights reserved
    =====================================================
    Update History:
    Ver.    TRB#            Date                Author        Note
    1        V001           2010/01/12          Toannn        Review lần 2 bị lổi
    1        V002           2010/02/04          Toannn        Thêm chức năng cập nhật lại Phương tiện khi Tracking    
    2.0     TRB003          2010/10/13          Tannd         Fix bug Zoom 2 level lớn nhất ở google lỗi     
*/
//alert(sLang);
var muststop     = GOOGLEFUNCS_MUST_STOP;
var showlist     = GOOGLEFUNCS_SHOW_LIST;
var hidelist     = GOOGLEFUNCS_HIDE_LIST;
var sorryg         = GOOGLEFUNCS_SORRY_G;
var alldevice     = GOOGLEFUNCS_ALL_DEVICE;
var f30t999     = GOOGLEFUNCS_30_999;
var connect     = GOOGLEFUNCS_CONNECT;
var nodata         = GOOGLEFUNCS_NO_DATA;
var dataerr     = GOOGLEFUNCS_DATA_ERR;
var stopfor     = GOOGLEFUNCS_STOPED;
var lostfor     = GOOGLEFUNCS_LOST_FOR;
var gpsfor         = GOOGLEFUNCS_LOST_GPS;
var unknown     = GOOGLEFUNCS_UNKNOW;
var devicename     = GOOGLEFUNCS_DEVICENAME;
var time         = GOOGLEFUNCS_TIME;
var ttime         = GOOGLEFUNCS_TTIME;
var speed         = GOOGLEFUNCS_STATUS;
var distance     = GOOGLEFUNCS_DISTANCE;
var distance_review     = GOOGLEFUNCS_DISTANCE_REVIEW;
var tdistance     = GOOGLEFUNCS_DISTANCE_TOTAL;
var strActive    = GOOGLEFUNCS_ACTIVE;
var satellite     = GOOGLEFUNCS_SATELLITE;
var moving         = GOOGLEFUNCS_MOVING;
var splay         = GOOGLEFUNCS_PLAY;
var sstop        = GOOGLEFUNCS_STOP;
var driver        = GOOGLEFUNCS_DRIVER;
var l            = GOOGLEFUNCS_LOCATION;
var latlong        = GOOGLEFUNCS_LAT_LONG

var new_loc        = GOOGLEFUNCS_ADD_NEW;
var new_name    = GOOGLEFUNCS_NEW_NAME;
var new_loc_type = GOOGLEFUNCS_NEW_LOC_TYPE;
var new_loc_type_select = GOOGLEFUNCS_SELECT_TYPE;
var new_address = GOOGLEFUNCS_ADDRESS;
var new_contact = GOOGLEFUNCS_CONTACT;
var new_phone = GOOGLEFUNCS_PHONE;
var new_amount = GOOGLEFUNCS_AMOUNT;
var new_images = GOOGLEFUNCS_IMAGE;
var new_radius    = GOOGLEFUNCS_RADIUS;
var units    = GOOGLEFUNCS_UNITS;    
var description    = GOOGLEFUNCS_REMARK;
var btnsave        = GOOGLEFUNCS_SAVE;
var btncancel        = GOOGLEFUNCS_CANCEL;

var map1 = GOOGLEFUNCS_MAP1;
var map2 = GOOGLEFUNCS_MAP2;
var map3 = GOOGLEFUNCS_MAP3;
var map4 = GOOGLEFUNCS_MAP4;
var map5 = GOOGLEFUNCS_MAP5;
//var vietBD = GOOGLEFUNCS_VIEPMAP;
var power_str = GOOGLEFUNCS_POWER;    // ĐậmPV: 2010-10-11 - Thêm trạng thái nguồn
var strinput1     = GOOGLEFUNCS_INPUT1;
var strinput2     = GOOGLEFUNCS_INPUT2;
var strinput3On     = GOOGLEFUNCS_INPUT3_ON;
var strinput3Off     = GOOGLEFUNCS_INPUT3_OFF;
var strinput4On     = GOOGLEFUNCS_INPUT4_ON;
var strinput4Off     = GOOGLEFUNCS_INPUT4_OFF;
var strinput_5On     = GOOGLEFUNCS_INPUT5_ON;
var strinput_5Off     = GOOGLEFUNCS_INPUT5_OFF;
//=================================================================================================
//start Trung add
var Gmap ;
var Gwaypoints ;
var Gmarker ;
var Gobjpolyline  ;
var drawMarker = null;
// end trung add
function GoogleFuncs()
{
    var obj=new Object();
    obj.latitudeCurrent=10;
    obj.longitudeCurrent;
    obj.levelCurrent;
    obj.map;
    obj.global=new Object();
    obj.arrIcon = new Array;
   // var map;
    
    var baseIcon;
    var baseIcon2;
    var baseIcon4; 
    //var icn;
    obj.locCreating;
    obj.labele;
    obj.wikiMoveEnd = null;
    obj.wikiZoomEnd = null;
    obj.currentMapName="";    
    obj.CreateIcon=function(newObj,nCheck,stt)
    {
        var name         = newObj.name;
        var drivers   = newObj.driver;
        var driver_phone = newObj.driverphone;
        var times        = newObj.time;
        var speeds       = newObj.speed;
        var distances = newObj.distance;
        var positions = newObj.position;
        var lats      = newObj.latitude;
        var lngs      = newObj.longitude;
        var statu     = newObj.status;
        var gps       = newObj.gps;
        var ids       = newObj.id;
        var links     = newObj.link;
        var T           = newObj.temperature;
        var A         = newObj.intensity;
        var satellite = newObj.satelline;
        var sumDistances= newObj.sumDistance;
        var input_5 = newObj.input5;
        var actives = newObj.active;
        var direction = newObj.direction;
        
        var infoWind="";
        var pnt = new GLatLng(lats, lngs);
        icn = new GIcon(baseIcon);
        icn.image=links;

        var infowind =  "";        
        var tilte="";
                
        var infowind =  '';
        obj.ClearDrawRun();
        // Chi co Theo Doi moi xem dc Trang Thai Nguon                    
        if(_global.TabSelect == 0)
        {
            tilte=name;
            var infoH = -1;
            infowind = '<div class="info" Style="width:230px;height:'+infoH+'px" align=\"left\">';
            if (_userinfos_.display_vehicle == 1) {
                infowind += devicename + name;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_driver == 1) {
                infowind += '<br/>' + driver + drivers + ' - ' + driver_phone;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_trktime == 1) {
                infowind += '<br/>' + time + times;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_status == 1) {
                infowind += '<br/>' + speed + speeds;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_km_day == 1) {
                infowind += '<br/>' + distance + distances;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_active == 1) {
                infowind += '<br/>' + strActive + (input_5 == 1 ? strinput_5On : strinput_5Off) +',  '+ actives;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_power == 1) {
                infowind += '<br />' + power_str + newObj.power_status;
                infoH = infoH + 13;
            }
            
            infowind += '</div>';
        } 
        else if(_global.TabSelect == 1) 
        {
            tilte=stt;
            var infoH = -1;
            infowind = '<div class="info" Style="width:230px;height:'+infoH+'px" align=\"left\">';
            if (_userinfos_.display_vehicle == 1) {
                infowind += devicename + name;
                infoH = infoH + 13;
            }
            if (_userinfos_.display_driver == 1) {
                infowind += '<br/>' + driver + drivers + ' - ' + driver_phone;
                infoH = infoH + 13;
            }
            if (_userinfos_.display_trktime == 1) {
                infowind += '<br/>' + time + times;
                infoH = infoH + 13;
            }
            if (_userinfos_.display_status == 1) {
                infowind += '<br/>' + speed + speeds;
                infoH = infoH + 13;
            }
            if (_userinfos_.display_distance == 1) {
                infowind += '<br/>' + distance_review + distances + ' Km. ';
                infoH = infoH + 13;
            }
            if (_userinfos_.display_km_day == 1) {
                if (_userinfos_.display_distance != 1) {
                    infowind += '<br/>';
                }
                infowind += tdistance + sumDistances + ' Km';
                infoH = infoH + 13;
            }            
            if (_userinfos_.display_active == 1) {
                infowind += '<br/>' + strActive + (input_5 == 1 ? strinput_5On : strinput_5Off) +',  '+ actives;
                infoH = infoH + 13;
            }
            
            infowind += '</div>';
        }        
        
        tilte=tilte.replace("-","_");
        var opts = 
                { 
                  icon: icn,
                  clickable: true,
                  title: tilte,
                  labelText: "<font color='red'>"+tilte+"</font>",
                  labelOffset: new GSize(-20,-20)
                };
        
            var marker2 = new LabeledMarker(pnt,opts);
            obj.map.addOverlay(marker2);

            if(nCheck==0)
                obj.SetInfo(marker2, infowind);
            if(nCheck==1)
                obj.SetInfoReview(marker2, infowind,stt);
        return marker2;
    }
    obj.ClearDrawRun = function()
    {
        if (drawMarker != null)
        {
            obj.map.removeOverlay(drawMarker);
        }   
    }
    obj.CreateDrawRun=function(lats, lngs)
    {
        obj.ClearDrawRun();
        var pnt = new GLatLng(lats, lngs);
        var icn2 = new GIcon(baseIcon4);
        icn2.image="images/transport/10.gif";
        var opts2 = 
                { 
                    icon: icn2,
                    clickable: true
                };        
        
        drawMarker = new GMarker(pnt,opts2);
        obj.map.addOverlay(drawMarker);
        return drawMarker;
    }
    obj.CreateIconDirection=function(newObj)
    {
        //alert('vo');
        var lats      = newObj.latitude;
        var lngs      = newObj.longitude;
        var direction = newObj.direction;
        var statu     = newObj.status;
        var pnt = new GLatLng(lats, lngs);
        var icn2 = new GIcon(baseIcon2);
        icn2.image="images/tracking/"+selectAngle(direction,statu);
        var opts2 = 
                { 
                    icon: icn2,
                    clickable: true
                };        
        
        var marker3 = new GMarker(pnt,opts2);
        obj.map.addOverlay(marker3);
        return marker3;
    }
    obj.SetInfo=function(marker, infoWind)
    {
        try
        {
            GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(infoWind);});
        }
        catch(e)
        {
        
        }        
    }
    obj.SetInfoReview=function(marker, infoWind,stt)
    {
        try
        {
            GEvent.addListener(marker, "click", function(){
                                                                marker.openInfoWindowHtml(infoWind);
                                                                _global.devReview.SetHighlight(stt);
                                                                _global.devReview.StatusR(stt);
                                                                _global.devReview.ScrollTop(stt);
                                                          });
        }
        catch(e)
        {
        
        }
        
    }
    obj.ShowInfo=function(newobj)
    {
        var name = newobj.name;
        var drivers1 = newobj.driver;
        var driver_phone = newobj.driverphone;
        var times = newobj.time;
        var speeds = newobj.speed;
        var distances = newobj.distance;
        var positions = newobj.position;
        var lats = newobj.latitude;
        var lngs = newobj.longitude;
        var T = newobj.temperature;
        var A = newobj.intensity;
        var statu = newobj.status;
        var satellite = newobj.satelline;
        var sumDistances= newobj.sumDistance;
        var gps = newobj.gps;
        var ids = newobj.id;
        var input_5 = newobj.input5;
        var actives = newobj.active;
        var latlng = new GLatLng(lats,lngs);         
        var power = newobj.power_status;    
        
        var infowind = '';
                        
        if(_global.TabSelect == 0)
        {                
            var infoH = -1;
            infowind = '<div class="info" Style="width:230px;height:'+infoH+'px" align=\"left\">';
            if (_userinfos_.display_vehicle == 1) {
                infowind += devicename + name;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_driver == 1) {
                infowind += '<br/>' + driver + drivers1 + ' - ' + driver_phone;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_trktime == 1) {
                infowind += '<br/>' + time + times;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_status == 1) {
                infowind += '<br/>' + speed + speeds;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_km_day == 1) {
                infowind += '<br/>' + distance + distances + ' Km';
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_active == 1) {
                infowind += '<br/>' + strActive + (input_5 == 1 ? strinput_5On : strinput_5Off) +', '+ actives;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_power == 1) {
                infowind += "<br/>" + power_str + power;
                infoH = infoH + 13;
            }
        }
        else
        {
            var infoH = -1;
            infowind = '<div class="info" Style="width:230px;height:'+infoH+'px" align=\"left\">';
            if (_userinfos_.display_vehicle == 1) {
                infowind += devicename + name;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_driver == 1) {
                infowind += '<br/>' + driver + drivers1 + ' - ' + driver_phone;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_trktime == 1) {
                infowind += '<br/>' + time + times;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_status == 1) {
                infowind += '<br/>' + speed + speeds;
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_distance == 1) {
                infowind += '<br/>' + distance_review + distances + ' Km. ';
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_km_day == 1) { 
                if (_userinfos_.display_distance != 1) {
                    infowind += '<br/>';
                }                
                infowind += tdistance + sumDistances + ' Km';
                infoH = infoH + 13;
            }
            
            if (_userinfos_.display_active == 1) {
                infowind += '<br/>' + strActive + (input_5 == 1 ? strinput_5On : strinput_5Off) +',  '+ actives;
                infoH = infoH + 13;
            }
        }
                
        infowind += '</div>';
                    
        obj.map.openInfoWindow(new GLatLng(lats,lngs),infowind)
    }
    obj.HideIcon=function(icon)
    {
        icon.hide();
    }
    
    obj.ShowIcon=function(icon)
    {
        try
        {
            if(typeof(icon)!='undefined')
            {
                icon.show();
            }    
        }
        catch(e)
        {
            alert(e);
        }
    }    
    obj.RemoveOverlay=function(icon)
    {
        //alert(icon);
        if(typeof(icon)!='undefined')
        {
            obj.map.removeOverlay(icon);
        }
    }
    obj.HideInfo=function()
    {
        obj.map.closeInfoWindow();
    }
    obj.UnloadMap=function()
    {        
        GUnload();       
    }
    
    obj.DrawLocationRec=function(lat1,lon1,lat2,lon2)
    {
        var p1 = new GLatLng(lat1,lon1);
        var p2 = new GLatLng(lat1,lon2);
        var p3 = new GLatLng(lat2,lon2);
        var p4 = new GLatLng(lat2,lon1);
        var arrLatLngs = new Array(p1, p2, p3, p4, p1);
                                
        var objPolygon = new GPolygon(arrLatLngs, "#ff0000",1,1,"#CCFFDF",0.2,{clickable:false});
        obj.map.addOverlay(objPolygon);
        return objPolygon;
    }
    
    obj.DrawLocationRou=function(lat,lng,r)
    {
        var center =new GLatLng(lat,lng);
        var    n = r;
        var radius = 4*n*1.609344;
        var latConv = 7347.086392356055;
        var lngConv = 7215.3469046674845;
        var nodes = 40;
        var points = [];
        var step = parseInt(360/nodes)||10;
        for(var i=0; i<=360; i+=step)
        {
            var pint = new GLatLng(lat + (radius/latConv * Math.cos(i* Math.PI/180)), lng + (radius/lngConv * Math.sin(i *Math.PI/180)));
            // push pints into points array
            points.push(pint);
        }
        polygon = new GPolygon(points, "#ff0000", 1, 1, "#CCFFDF",0.2,{clickable:false});
        obj.map.addOverlay(polygon);
        return polygon;
    }
    obj.ShowFormLocation=function(lat,lng,strHTML)
    {        
        obj.map.openInfoWindow(new GLatLng(lat,lng),strHTML);                                            
    }
    obj.DeleteLocation=function(polygon)
    {
        obj.map.removeOverlay(polygon);
    }
    
    obj.ShowLocation=function(polygon)
    {
        polygon.show();
    }
    obj.HideLocation=function(polygon)
    {
        polygon.hide();
    }
    
    // Add by Lamdhn: YC101 - 2011.04.13
    obj.PolyDragControl = null;
    obj.InitMap =function(div, type)
    {
        type = type || 0;        // ĐậmPV: 2010-10-27 - Add: 0: Gmaps, 1: vngeobiz
        if (GBrowserIsCompatible()) 
        {
            obj.map = new GMap2(document.getElementById(div));
            
            //obj.map.addControl(new TextualZoomControl()); 
            //map.setUIToDefault();
            //16.2152328, 106.280505
            obj.map.addControl(new GScaleControl());
            obj.map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 25)));
            baseIcon = new GIcon(G_DEFAULT_ICON);
            baseIcon.shadow = '';
            baseIcon.iconSize = new GSize(22, 22);
            baseIcon.iconAnchor = new GPoint(11, 11);
            baseIcon.infoWindowAnchor = new GPoint(11, 11);
            baseIcon2 = new GIcon(G_DEFAULT_ICON);
            baseIcon2.shadow = '';
            baseIcon2.iconSize = new GSize(22, 22);
            baseIcon2.iconAnchor = new GPoint(10, 35);
            //baseIcon2.infoWindowAnchor = new GPoint(10, 18);    
            
            baseIcon4 = new GIcon(G_DEFAULT_ICON);
            baseIcon4.shadow = '';
            baseIcon4.iconSize = new GSize(32, 32);
            baseIcon4.iconAnchor = new GPoint(16, 16);        
            //============================= chuyen thanh ban do tieng viet ==============================
            
            obj.map.addControl(new GMapTypeControl());
            obj.map.enableScrollWheelZoom();
            
            // add custommap and orverlay wikimapia start
            // Set up the copyright information
            var myCopyright = new GCopyrightCollection("© ");
            myCopyright.addCopyright(new GCopyright('Demo',new GLatLngBounds(new GLatLng(-90,-180), 
                        new GLatLng(90,180)),
                        0,'©2007 Google'));
            //********************************
            // VBD on google
            var vdbCopyright = new GCopyrightCollection("Mapdata");
           vdbCopyright.addCopyright(new GCopyright('VietBanDo',
             new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
             0,'@2009 VietBanDo')); 
          
           //******************************
           var VBD_tilelayer1 = new GTileLayer(vdbCopyright);
                    //VBD_tilelayer1.getTileUrl = GetVBDTile;
                    VBD_tilelayer1.isPng = function() { return true;};
                    VBD_tilelayer1.getOpacity = function() { return 1.0; }
           var Only_VBD1 = [VBD_tilelayer1];
           var GMapTypeOptions = new Object();
            GMapTypeOptions.minResolution = 0;
            GMapTypeOptions.maxResolution = 20;
            GMapTypeOptions.errorMessage = "No map data available";
           //var maptype_VBD1 = new GMapType(Only_VBD1, new GMercatorProjection(22),vietBD,GMapTypeOptions);
            //**********************************
            // Create the tile layer overlay and             
            var wiki_tilelayer = new GTileLayer(myCopyright);
            wiki_tilelayer.getTileUrl = obj.GetTileUrlWikimapia;
            wiki_tilelayer.isPng = function() { return true;};
            wiki_tilelayer.getOpacity = function() { return 1.0; }

            //var myTileLayerOverplay = new GTileLayerOverlay(tilelayer);
            //obj.map.addOverlay(myTileLayerOverplay);
            //end add custommap and overlay wikimapia
                
            if(type == 0)
            {
                var crossLayer = new GTileLayer(new GCopyrightCollection(""), 0, 15);
                crossLayer.getTileUrl =  function(tile, zoom) {
                return "./include/tile_crosshairs.png";
                }
                crossLayer.isPng = function() 
                {
                    return true;
                }
                var tilelayers1 = [G_NORMAL_MAP.getTileLayers()[0],crossLayer];
                var tilelayers2 = [G_HYBRID_MAP.getTileLayers()[0],crossLayer,G_HYBRID_MAP.getTileLayers()[1]];
                var tilelayers3 = [G_SATELLITE_MAP.getTileLayers()[0],crossLayer];
                var tilelayers4 = [G_PHYSICAL_MAP.getTileLayers()[0],crossLayer];
                var Satelllite_Wiki = [G_SATELLITE_MAP.getTileLayers()[0],wiki_tilelayer];
                // var tilelayers5 = [G_WIKI_MAP.getTileLayers()[0],crossLayer];
                // TRB003 Start edit
                //var NORMAL = new GMapType(tilelayers1, new GMercatorProjection(20),map1);
                //var SATELLITE = new GMapType(tilelayers3, new GMercatorProjection(20),map2);  
                //var HYBRID = new GMapType(tilelayers2, new GMercatorProjection(20),map3);
                //var PHYSICAL = new GMapType(tilelayers4, new GMercatorProjection(20),map4);
                ///////////////////////////////
                // Phải cho phép zoom đến mức 22
                var NORMAL = new GMapType(tilelayers1, new GMercatorProjection(22),map1);
                var SATELLITE = new GMapType(tilelayers3, new GMercatorProjection(22),map2);  
                var HYBRID = new GMapType(tilelayers2, new GMercatorProjection(22),map3);
                var PHYSICAL = new GMapType(tilelayers4, new GMercatorProjection(22),map4);
                var WIKI = new GMapType(Satelllite_Wiki, new GMercatorProjection(22),map5);
                // TRB003 End Edit
                var mapdetail = $("#hd_mapdetail").val();
                obj.map.addMapType(NORMAL);
                obj.map.addMapType(SATELLITE);
                obj.map.addMapType(HYBRID);
                if (mapdetail == 15)
                { 
                obj.map.addMapType(PHYSICAL);
                }
                //obj.map.addMapType(maptype_VBD1); 
                obj.map.addMapType(WIKI); 

                obj.map.setCenter(new GLatLng(10.7654865, 106.67793273), 15);
            }
            else
            {
                // Create the copyright collection
                var copyCollection = new GCopyrightCollection('RasterMap');
                var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(21.0229825, 105.8422852), new GLatLng(21.0332372, 105.8587646)), 0, "Created by Global Mapper");
                copyCollection.addCopyright(copyright);
                
                // Setup the tile layers
                var tilelayers = new Array();
                //tilelayers[0] = G_SATELLITE_MAP.getTileLayers()[0];
                tilelayers[0] = new GTileLayer(copyCollection, 0, 20 );
                tilelayers[0].getTileUrl = CustomGetTileUrl
                tilelayers[0].isPng = function() {return true;};
                
                // Setup the options for our map layer
                var GMapTypeOptions = new Object();
                GMapTypeOptions.minResolution = 0;
                GMapTypeOptions.maxResolution = 20;
                GMapTypeOptions.errorMessage = "No map data available";
                
                // Create our custom map layer
                var custommap = new GMapType(tilelayers, new GMercatorProjection(22), "ada", GMapTypeOptions);
                custommap.getTextColor = function() {return "#000000";};
                obj.map.addMapType(custommap);
                obj.map.setCenter(new GLatLng(10.7654865, 106.67793273), 13, custommap);
            }    
            obj.map.removeMapType(G_NORMAL_MAP);
            obj.map.removeMapType(G_HYBRID_MAP);
            obj.map.removeMapType(G_SATELLITE_MAP);
            obj.map.removeMapType(G_PHYSICAL_MAP);
           // obj.map.removeMapType(G_WIKI_MAP);
            //=====================================================================================
            obj.loaded=true;    
            // start trung add
            Gmap = obj.map;
            Gwaypoints =new Array();
            Gmarker = null;
            Gobjpolyline = new Array() ;
            // end trung add
            
            // start phucnh 
             //GEvent.addListener(Gmap, 'zoomend', function(oldl, newl)
//                { 
//                    document.getElementById('hdZoomto').value = newl; 
//                    document.getElementById('hdZoomOld').value = oldl;                             
//                    if ((_global.TabSelect == 1) && ($('#chkFlgPaint').val() == 3))   
//                    {
//                         _global.devReview.flgPaintOnchanged(3);                 
//                   }
//                });
             GEvent.addListener(Gmap, 'infowindowclose', function()
                {
                   if (_global.IsPlaying == false)
                   {
                    document.getElementById('hdMoveEnd').value = 0;
                   }
                   else
                   {
                    document.getElementById('hdMoveEnd').value = 1;
                   }
                });     
            // end phucnh
            //Phuong add bắt sự kiên wiki changed
            //start lấy sự kiên changemap
            GEvent.addListener(Gmap, 'maptypechanged', function()
                {              
                    var maptype = obj.map.getCurrentMapType();
                    //alert(maptype);
                    obj.currentMapName = maptype.getName(false);
                    if(obj.currentMapName == map5)
                    {                            
                        obj.ShowWiki();
                        if( obj.wikiMoveEnd==null)
                        {
                            obj.wikiMoveEnd = GEvent.addListener(obj.map, 'moveend', function()
                            {                          
                                obj.ShowWiki();
                            });
                        }
                        if(obj.wikiZoomEnd==null)
                        {
                            obj.wikiZoomEnd = GEvent.addListener(obj.map, 'zoomend', function(oldl, newl)
                            {               
                                obj.ShowWiki();
                            });     
                        }                   
                    }
                    else
                    {                       
                        if(obj.wikiMoveEnd != null)
                        {
                            GEvent.removeListener(obj.wikiMoveEnd);     
                            obj.wikiMoveEnd=null;                                                   
                        }
                        if(obj.wikiZoomEnd != null)
                        {
                            GEvent.removeListener(obj.wikiZoomEnd);
                            obj.wikiZoomEnd=null;
                        }
                        obj.StopWiki();
                    }   
                });
            //end lấy sự kiện changemap
            //end Phuong add sự kiên wiki changed
            GEvent.addListener(Gmap, "click", function(overlay,point)
             {                
            
                if(typeof(point)!="undefined")
                { 
                    
                    if(Gmarker==null)
                        Gmarker = new GMarker(point);               
                        Gwaypoints.push(point);        
                        var Gpolyline = new GPolyline(Gwaypoints, 'red',5);
                        obj.map.addOverlay(Gmarker);
//                        obj.map.addOverlay(Gpolyline);                          
//                        Gobjpolyline.push(Gpolyline); 
                    // start trung add
                    if(document.getElementById('mapType').value ==0){
                    //if(document.getElementById('flagDistance').value==1){
//                        
//                        if(Gmarker==null)
//                        Gmarker = new GMarker(point);               
//                        Gwaypoints.push(point);        
//                        var Gpolyline = new GPolyline(Gwaypoints, 'red',5);
//                        obj.map.addOverlay(Gmarker);
//                        obj.map.addOverlay(Gpolyline);                          
//                        Gobjpolyline.push(Gpolyline);                                      
//                        var long = Gpolyline.getLength();
//                        long = Math.round(long);
//                        var unit = $('#UnitDistance').val();
//                        $('#hdUnitValue').val(unit);
//                        long = long/unit;
//                        long=CurrentFormatted(long);
//                        document.getElementById('distanceValue').value = long;
//                       
//                    }
                    
                }
                // end trung add
                //_global.locs.Infopolygon(point);
             }    
             });
             
        }
        else
        {
            alert(sorryg);
        }
        
        // Add-Start by Lamdhn: YC101 - 2011.04.13
        obj.PolyDragControl = new MPolyDragControl({map:obj.map, type:'rectangle'});
        obj.PolyDragControl.disableTransMarker();
        // Add-End by Lamdhn: YC101 - 2011.04.13 
            
         //GEvent.addListener(Gmap, 'moveend', function()
//            {   

//                if(!_global.locs.flgProcess){
//                      _global.locs.flgProcess= true;
//                      _global.locs.panLocation();
//                 }  
//              

//            });

    }
    // trung add
    
    // end trung add 
    obj.ZoomTo = function(nLat, nLong, nLevel)
    {
        obj.map.setCenter(new GLatLng(nLat, nLong),nLevel);
    }
    obj.MoveEnd = function(nLat, nLong)
    {
        var g_LatLngBound = obj.map.getBounds();
        if (g_LatLngBound.containsLatLng(new GLatLng(nLat, nLong)) == false)
        {
           obj.map.setCenter(new GLatLng(nLat, nLong));    
        }
    }
    
    obj.PanTo = function(nLat, nLong)
    {
        //obj.map.panTo(new GLatLng(nLat, nLong));
        obj.map.setCenter(new GLatLng(nLat, nLong));
    }

    // Add-Start by Lamdhn: YC101 - 2011.04.05 
    obj.CancelNewLocByRec = function() {
        obj.PolyDragControl.reset();
        
        obj.HideInfo(); 
    }
    
    obj.SaveNewLocByRec = function() {
       // _global.locs.SaveLocByRec()
    }
    
    obj.CancelEditLocByRec = function() {
        obj.PolyDragControl.reset();
        obj.PolyDragControl.ondragend = obj.AddOnClickByRec;
        obj.DisablePolyDrag();
    }
    
    obj.SaveEditLocByRec = function() {
        //_global.locs.ProcessSaveLocByRec();
        obj.PolyDragControl.reset();
        obj.DisablePolyDrag();
    }
    
    obj.DisablePolyDrag = function() {
        obj.HideInfo();                    
        if (!document.getElementById('chkAddLoc').checked || !document.getElementById('rdLocRec').checked) {
            if (obj.PolyDragControl) {
                obj.PolyDragControl.disableTransMarker();
            }
        }
    }
    
    obj.DataLocEdit = [];
    obj.EditOnClickByRec = function() {
        var point_first;
        var point_last;
        var lat_center;
        var lng_center;
        if(obj.DataLocEdit['draw_index'] == 0) {
            if (obj.PolyDragControl.bounds == null) {
                return;
            }
        
            point_first = obj.PolyDragControl.bounds.getSouthWest();
            point_last = obj.PolyDragControl.bounds.getNorthEast(); 
            lat_center = (parseFloat(point_last.lat()) + parseFloat(point_first.lat()))/2;
            lng_center = (parseFloat(point_last.lng()) + parseFloat(point_first.lng()))/2; 
            //alert(lat_center);
           // lng_center =  ((float)point_last.lng()+ (float)point_first.lng())/2;  
 
        } else {
            obj.PolyDragControl.disableTransMarker();
            point_first = new GLatLng(obj.DataLocEdit['lat1'], obj.DataLocEdit['lng1']);
            point_last = new GLatLng(obj.DataLocEdit['lat2'], obj.DataLocEdit['lng2']);
            lat_center = (parseFloat(obj.DataLocEdit['lat1']) + parseFloat(obj.DataLocEdit['lat2']))/2;
            lng_center = (parseFloat(obj.DataLocEdit['lng1']) + parseFloat(obj.DataLocEdit['lng2']))/2; 
           // alert(lat_center);
            //lat_center = ((float) obj.DataLocEdit['lat1']+ obj.DataLocEdit['lat2'])/2;
            //lng_center =  ((float)obj.DataLocEdit['lng1'] + obj.DataLocEdit['lng2'])/2;      
        }
        
        var strHTML = '<div class = "info" style="width:280px; height:210px;z-index:0; margin-top:0px;" >'+new_loc+
                    '<from id ="VM"  style="margin-top:0px;" >'+
                        '<table style="margin-top:0px;" border=0 cellpadding=1 cellspacing=1>'+
                            '<tr>'+
                                '<td width="100px" align="right">'+new_name+'</td>'+
                                '<td width="150px"><input type="text" name="txtLocNameByEdit" id="txtLocNameByEdit" value="'+obj.DataLocEdit['name']+'" maxlength="60" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_address+'</td>'+
                                '<td><input type="text" name="txtAddressByEdit" id="txtAddressByEdit" value="'+obj.DataLocEdit['address']+'" maxlength="200" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_loc_type+'</td>'+
                                '<td>'+
                                '<select name="ddlLocTypeByEdit" id="ddlLocTypeByEdit" style="width:152px;">'+
                                '<option value="">'+new_loc_type_select+'</option>';
                                for (var i=0; i<_arrLocationType.length; i++) {
                                    if (obj.DataLocEdit['loctype'] == _arrLocationTypeId[i]) {
                                        strHTML += '<option value="'+_arrLocationTypeId[i]+'" selected>'+_arrLocationType[i]+'</option>';
                                    } else {
                                        strHTML += '<option value="'+_arrLocationTypeId[i]+'">'+_arrLocationType[i]+'</option>';    
                                    }                                    
                                }
                    strHTML += '</select>'+
                                '</td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_contact+'</td>'+
                                '<td><input type="text" name="txtContactByEdit" id="txtContactByEdit" value="'+obj.DataLocEdit['contact']+'" maxlength="200" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_phone+'</td>'+
                                '<td><input type="text" name="txtPhoneByEdit" id="txtPhoneByEdit" value="'+obj.DataLocEdit['phone']+'" maxlength="20" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_amount+'</td>'+
                                '<td><input type="text" size="2" name="txtFeeByEdit" id="txtFeeByEdit" value="'+obj.DataLocEdit['amount']+'" style="width: 150px;"></td>'+
                            '</tr>'+                                        
                            '<tr>'+
                                '<td align="right">'+description+'</td>'+
                                '<td><textarea name="txtDescByEdit" id="txtDescByEdit" maxlength="225" style="width: 150px; height: 40px; border: solid 1px green; overflow: auto;">'+obj.DataLocEdit['description']+'</textarea></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td></td>'+
                                '<td align="left">'+
                                    '<input type="submit" onclick="_global.myMap.SaveEditLocByRec();" value="'+btnsave+'" name="B1">'+
                                    '<input type="reset" value="'+btncancel+'" name="B2" onclick="_global.myMap.CancelEditLocByRec();">'+
                                '</td>'+
                            '</tr>'+
                        '</table>'+
                    '<input type="hidden" name="hdnLocIdByEdit"  id="hdnLocIdByEdit"  value='+obj.DataLocEdit['id']+'>'+
                    '<input type="hidden" name="hdnLat1ByEdit"  id="hdnLat1ByEdit"  value='+point_first.lat()+'>'+
                    '<input type="hidden" name="hdnLng1ByEdit"  id="hdnLng1ByEdit"  value='+point_first.lng()+'>'+
                    '<input type="hidden" name="hdnLat2ByEdit"  id="hdnLat2ByEdit"  value='+point_last.lat()+'>'+
                    '<input type="hidden" name="hdnLng2ByEdit"  id="hdnLng2ByEdit"  value='+point_last.lng()+'>'+
                    '</from>'+
                    '</div>';
                        
        if (obj.PolyDragControl) {
            obj.PolyDragControl.ondragend = obj.EditOnClickByRec;     
        }
        var  point_center = new GLatLng(lat_center,lng_center);  
        obj.map.openInfoWindow(point_center, strHTML); 
    }
                    
    obj.AddOnClickByRec = function() {
        if (obj.PolyDragControl.bounds == null) {
            return;
        }
        
        var point_first = obj.PolyDragControl.bounds.getSouthWest();
        var point_last = obj.PolyDragControl.bounds.getNorthEast();
    
        var strHTML = '<div class = "info" style="width:280px; height:210px;z-index:0; margin-top:0px;" >'+new_loc+
                    '<from id ="VM"  style="margin-top:0px;" >'+
                        '<table style="margin-top:0px;" border=0 cellpadding=1 cellspacing=1>'+
                            '<tr>'+
                                '<td width="100px" align="right">'+new_name+'</td>'+
                                '<td width="150px"><input type="text" name="txtLocNameByAddRec" id="txtLocNameByAddRec" value="" maxlength="60" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_address+'</td>'+
                                '<td><input type="text" name="txtAddressByAddRec" id="txtAddressByAddRec" value="" maxlength="200" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_loc_type+'</td>'+
                                '<td>'+
                                '<select name="ddlLocTypeByAddRec" id="ddlLocTypeByAddRec" style="width:152px;">'+
                                '<option value="">'+new_loc_type_select+'</option>';
                                for (var i=0; i<_arrLocationType.length; i++) {
                                    strHTML += '<option value="'+_arrLocationTypeId[i]+'">'+_arrLocationType[i]+'</option>';
                                }
                    strHTML += '</select>'+
                                '</td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_contact+'</td>'+
                                '<td><input type="text" name="txtContactByAddRec" id="txtContactByAddRec" value="" maxlength="200" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_phone+'</td>'+
                                '<td><input type="text" name="txtPhoneByAddRec" id="txtPhoneByAddRec" value="" maxlength="20" style="width: 150px;"></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td align="right">'+new_amount+'</td>'+
                                '<td><input type="text" size="2" name="txtFeeByAddRec" id="txtFeeByAddRec" value="" style="width: 150px;"></td>'+
                            '</tr>'+                                        
                            '<tr>'+
                                '<td align="right">'+description+'</td>'+
                                '<td><textarea name="txtDescByAddRec" id="txtDescByAddRec" maxlength="225" style="width: 150px; height: 40px; border: solid 1px green; overflow: auto;"></textarea></td>'+
                            '</tr>'+
                            '<tr>'+
                                '<td></td>'+
                                '<td align="left">'+
                                    '<input type="submit" onclick="_global.myMap.SaveNewLocByRec();" value="'+btnsave+'" name="B1">'+
                                    '<input type="reset" value="'+btncancel+'" name="B2" onclick="_global.myMap.CancelNewLocByRec();">'+
                                '</td>'+
                            '</tr>'+
                        '</table>'+
                    '<input type="hidden" name="hdnLat1ByAddRec"  id="hdnLat1ByAddRec"  value='+point_first.lat()+'>'+
                    '<input type="hidden" name="hdnLng1ByAddRec"  id="hdnLng1ByAddRec"  value='+point_first.lng()+'>'+
                    '<input type="hidden" name="hdnLat2ByAddRec"  id="hdnLat2ByAddRec"  value='+point_last.lat()+'>'+
                    '<input type="hidden" name="hdnLng2ByAddRec"  id="hdnLng2ByAddRec"  value='+point_last.lng()+'>'+
                    '</from>'+
                    '</div>';
        
                                                                           
        obj.map.openInfoWindow(point_last, strHTML);
        // 2011-07-27: Đậm Add Start
        // Lấy infowindows vừa mở
        var info = obj.map.getInfoWindow();
        GEvent.addListener(info, 'closeclick', function(){
            // Xóa hình chữ nhật đã vẽ
            _global.myMap.CancelNewLocByRec();
            document.VM.reset();
        });
        // 2011-07-27: Đậm Add End
    }   
    // Add-End by Lamdhn: YC101 - 2011.04.05
    
    obj.AddOnClick = function()
    {    
        var objBind = GEvent.addListener(obj.map, "click", function(overlay, pt)
        {
            if(typeof(pt)!='undefined')
            {    
                var rec=obj.CreateRec(pt.lat(),pt.lng(),1);
                
                var strHTML = '<div class = "info" style="width:280px; height:225px;z-index:0; margin-top:0px;" >'+new_loc+
                                '<from id ="VM"  style="margin-top:0px;" >'+
                                    '<table style="margin-top:0px;" border=0 cellpadding=1 cellspacing=1>'+
                                        '<tr>'+
                                            '<td width="100px" align="right">'+new_name+'</td>'+
                                            '<td width="150px"><input type="text" name="txtLocName" id="txtLocName" value="" maxlength="60" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_address+'</td>'+
                                            '<td><input type="text" name="txtAddress" id="txtAddress" value="" maxlength="200" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_loc_type+'</td>'+
                                            '<td>'+
                                            '<select name="ddlLocType" id="ddlLocType" style="width:152px;">'+
                                            '<option value="">'+new_loc_type_select+'</option>';
                                            for (var i=0; i<_arrLocationType.length; i++) {
                                                strHTML += '<option value="'+_arrLocationTypeId[i]+'">'+_arrLocationType[i]+'</option>';
                                            }
                                strHTML += '</select>'+
                                            '</td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_contact+'</td>'+
                                            '<td><input type="text" name="txtContact" id="txtContact" value="" maxlength="200" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_phone+'</td>'+
                                            '<td><input type="text" name="txtPhone" id="txtPhone" value="" maxlength="20" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_amount+'</td>'+
                                            '<td><input type="text" size="2" name="txtFee" id="txtFee" value="" style="width: 150px;"></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+new_radius+'</td>'+
                                            '<td><input type="text" size="2" name="txtRad" id="txtRad" value="1" style="width: 40px;" maxlength="2"> '+units+' 100m</td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td align="right">'+description+'</td>'+
                                            '<td><textarea name="txtDesc" id="txtDesc" maxlength="225" style="width: 150px; height: 40px; border: solid 1px green; overflow: auto;"></textarea></td>'+
                                        '</tr>'+
                                        '<tr>'+
                                            '<td></td>'+
                                            '<td align="left">'+
                                                '<input type="submit" onclick="_global.locs.SaveLoc()" value="'+btnsave+'" name="B1">'+
                                                '<input type="reset" value="'+btncancel+'" name="B2" onclick="_global.myMap.HideInfo();_global.myMap.DeleteLocation(_global.myMap.locCreating);">'+
                                            '</td>'+
                                        '</tr>'+
                                    '</table>'+
                                '<input type="hidden" name="clickLat"  id="clickLat"  value='+pt.lat()+'>'+
                                '<input type="hidden" name="clickLng"  id="clickLng"  value='+pt.lng()+'>'+
                                '</from>'+
                              '</div>';
                              
                obj.locCreating=obj.DrawLocationRec(rec.lat1,rec.lng1,rec.lat2,rec.lng2);
                obj.map.openInfoWindow(pt,strHTML);
                // 2011-07-27: Đậm Add Start
                // Lấy infowindows vừa mở
                var info = obj.map.getInfoWindow();
                GEvent.addListener(info, 'closeclick', function(){
                    // Xóa hình vuông đã vẽ
                    _global.myMap.HideInfo();
                    _global.myMap.DeleteLocation(_global.myMap.locCreating);
                });
                // 2011-07-27: Đậm Add End
            }            
        }                
        );        
        return(objBind);
    }
    
    obj.RemoveOnClick = function(myEventListener)
    {
        GEvent.removeListener(myEventListener);    
    }
    obj.Point=function(nLat,nLong)
    {
        return (new GLatLng(nLat,nLong));
    }
    obj.CheckHide=function(icon)
    {
        if(icon.isHidden()==false)
            return false;
        else 
            return true;
    }
    obj.GetLevel=function()
    {
        return obj.map.getZoom();
    }
    obj.GetCenter=function() //obj.latitude, longitude
    {
        var result=new Object();
        var p= obj.map.getCenter(); 
        result.latitude=p.lat();
        result.longitude= p.lng(); 
        return result;
    }    
    obj.ClearOverlay=function()
    {
        obj.map.clearOverlays();
    //    alert("clear");
    }
    obj.CreateLocal = function(latclick,lngclick)
    {
        var radius = 4*1*1.609344;
        var latConv = 7347.086392356055;
        var    lngConv = 7215.3469046674845;
        var point1 = new GLatLng(latclick + (radius/latConv * Math.cos(0* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(0 *Math.PI/180)));                                
        var point2 = new GLatLng(latclick + (radius/latConv * Math.cos(90* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(90 *Math.PI/180)));
        var point3 = new GLatLng(latclick + (radius/latConv * Math.cos(180* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(180 *Math.PI/180)));                                
        var point4 = new GLatLng(latclick + (radius/latConv * Math.cos(270* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(270 *Math.PI/180)));                                                
        
        var p1 = new GLatLng(point1.lat(),point4.lng());
        var p2 = new GLatLng(point1.lat(),point2.lng());
        var p3 = new GLatLng(point3.lat(),point2.lng());
        var p4 = new GLatLng(point3.lat(),point4.lng());
        
        obj.PanTo(latclick,lngclick);
        var arrLatLngs = new Array(p1, p2, p3, p4, p1);
        var polygon = new GPolygon(arrLatLngs, "#ff0000",1,1,"#CCFFDF",0.2,{clickable:false});
        obj.map.addOverlay(polygon);
    }
    obj.CreateRec = function(lat,lng,rad)
    {
        if(_global.myMap.locCreating)
            _global.myMap.DeleteLocation(_global.myMap.locCreating);
            
        var latclick = parseFloat(lat);
        var lngclick = parseFloat(lng);

        var result=new Object();
        var radius = 4*rad*1.609344;
        var latConv = 7347.086392356055;
        var    lngConv = 7215.3469046674845;
        var point1 = new GLatLng(latclick + (radius/latConv * Math.cos(0* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(0 *Math.PI/180)));                                
        var point2 = new GLatLng(latclick + (radius/latConv * Math.cos(90* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(90 *Math.PI/180)));
        var point3 = new GLatLng(latclick + (radius/latConv * Math.cos(180* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(180 *Math.PI/180)));                                
        var point4 = new GLatLng(latclick + (radius/latConv * Math.cos(270* Math.PI/180)), lngclick + (radius/lngConv * Math.sin(270 *Math.PI/180)));    
        
        result.lat1=point1.lat();
        result.lng1=point4.lng();
        result.lat2=point3.lat();
        result.lng2=point2.lng();
        return result;
    }
    obj.Contains = function(polygon,point)
    {
        var j=0;
        var oddNodes = false;
        var x = point.lng();
        var y = point.lat();
        if(typeof(polygon)!='undefined')
        {    
            for (var i=0; i < polygon.getVertexCount(); i++) {
              j++;
              if (j == polygon.getVertexCount()) {j = 0;}
              if (((polygon.getVertex(i).lat() < y) && (polygon.getVertex(j).lat() >= y))
              || ((polygon.getVertex(j).lat() < y) && (polygon.getVertex(i).lat() >= y))) {
                if ( polygon.getVertex(i).lng() + (y - polygon.getVertex(i).lat())
                /  (polygon.getVertex(j).lat()-polygon.getVertex(i).lat())
                *  (polygon.getVertex(j).lng() - polygon.getVertex(i).lng())<x ) {
                  oddNodes = !oddNodes
                }
              }
            }
        }    
        return oddNodes;
      }
      obj.Makeinfo = function(index)
    {    
        icn = new GIcon(baseIcon);
        icn.image='images/spacer.gif';
       
        var info = index.name;
        info=ToName(info);
        var latlng = new GLatLng(index.lat1,index.lng1);     
        var opts = { 
                  icon: icn,
                  //clickable: true,
                  title:info,
                  labelText: "<font size ='2' color='blue'> "+info+"</font>",
                  labelOffset: new GSize(0,-15)
                };
        var marker = new LabeledMarker(latlng,opts);
        obj.map.addOverlay(marker);
        
        return marker;            
    }
    function ToName(name)
    {
        try
        {    
            var arr1 = new Array();
            var str='';
            arr1 = name.split(" ");
            for(i=0;i<arr1.length-1;i++)
                str+=arr1[i]+"_";
            str+=arr1[arr1.length-1];
            return str;
        }
        catch(e)
        {
            return name;
        }
    }
    obj.CreateIconMap=function(newObj)
    {
        
        var name         = newObj.name;
        
        var lats1      = newObj.lat1;
        var lngs1     = newObj.lng1;
        var lats2      = newObj.lat2;
        var lngs2     = newObj.lng2;        
        var links     = newObj.link;
        
        var lat = (lats1 + lats2)/2;
        var lng = (lngs1 + lngs2)/2;
        
        
        var pnt = new GLatLng(lat, lng);

        icn = new GIcon(baseIcon);
        icn.image=links;    
        
        var tilte = name;
        
        tilte=tilte.replace(" ","_");
        
        var textname ='';          
        if(obj.GetLevel()>=15){
           textname =  "<font color='blue'>"+tilte+"</font>";
        }       
        
        var opts = 
            { 
              icon: icn,
              clickable: true,
              title: tilte,
              labelText: textname,
              labelOffset: new GSize(-20,0)
            };
            
        var marker2 = new LabeledMarker(pnt,opts);
        obj.map.addOverlay(marker2);
        
        return marker2;        
    }
    obj.DrawPolyline=function(latnew,longnew,latold,longold)
    {
        
         var polyline = new GPolyline([
            new GLatLng(latnew,longnew),
            new GLatLng(latold,longold)
            ], "#ff0000", 3);
        obj.map.addOverlay(polyline);
        return polyline;
    }
    //****************************************************************************************************************
    // region wiki     
    obj.aname = new Array();
    obj.arrIdWikiObj = new Array();
    obj.arrPolygonWiki = new Array();
    obj.posWiki = 0;
    obj.GetTileUrlWikimapia=function (a,b)
    { 
      var i=a.x%4;
      var j=a.y%4;
      var temp=j*4+i;  
      return "http://i"+temp+".wikimapia.org/?x="+a.x+"&y="+a.y+"&zoom="+b+"&type=hybrid&lng=38";
    }
    obj.StopWiki=function()
    {
        //$("#divwiki").css("display",'none'); 
        var i = 0;
         for(i = 0; i < obj.arrPolygonWiki.length; i++)
         {
             GEvent.clearListeners(obj.arrPolygonWiki[i],"mousemove");
             GEvent.clearListeners(obj.arrPolygonWiki[i],"mouseout");
             Gmap.removeOverlay(obj.arrPolygonWiki[i]);
             //Gmap.clearOverlays();
         }
              
         $(".tooltipmap").css("visibility","hidden");     
         obj.aname = new Array();
         obj.arrPolygonWiki = new Array();   
         obj.arrIdWikiObj = new Array();     
         obj.posWiki = 0;
         //$("#divwiki").css("display",'');
             
    }
    //Cross domain
    obj.CreateCrossRequest=function(method, url)
    {
        var xhr = new XMLHttpRequest();
        //alert("xmlrequest");
        if ("withCredentials" in xhr){
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined"){
            //alert("xdomain");
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            xhr = null;
        }
        return xhr;
    }
    obj.ShowWiki=function()
    {
        //$("#divwiki").css("display",'none');
        var bounds = Gmap.getBounds();
        var sw = bounds.getSouthWest();
        var ne = bounds.getNorthEast();   
        //alert("lon_min=" + sw.lng() + "&lon_max=" + ne.lng() + "&lat_min=" + sw.lat() + "&lat_max=" + ne.lat());
        var count = 500;
        var page = 1; 
        var xmlhttp = null;        
        var st = "http://api.wikimapia.org/?function=box&key=3AAAD569-8E95440A-1EB7F4A0-67294FA7-48F26AB9-F3212F16-E593AF24-41A1508&lon_min=" + sw.lng() + "&lon_max=" + ne.lng() + "&lat_min=" + sw.lat() + "&lat_max=" + ne.lat() + "&format=json&page=" + page + "&count=" + count;
        var request = obj.CreateCrossRequest("get",st);
        if (request)
        {
            request.onload = function()
            {
                //alert(request.responseText);  
                 if(obj.currentMapName != map5)
                 {
                     return;
                 }                     
                var json = request.responseText;
                if(typeof(json) != 'object')
                {  
                    json = $.parseJSON(json);
                }               
                json = json.folder;          
                var i = 0;
                var j = 0;
                for(i = 0; i < json.length; i++)
                {// FakeWiki
                    // Kiem tra ton tai                       
                    if(obj.arrIdWikiObj.indexOf(json[i].id) == -1)
                    {
                        obj.arrIdWikiObj[obj.posWiki] = json[i].id;
                        obj.aname[obj.posWiki] = json[i].name;
                        // Get location
                        var poly_points = json[i].polygon;
                        var points = new Array();
                        for(j = 0; j < poly_points.length; j++)
                        {
                            points[j] = new GLatLng(poly_points[j].y, poly_points[j].x);
                        }
                        obj.arrPolygonWiki[obj.posWiki] = new GPolygon(points, '#FFFFFF', 1, 1, '#FF0000', 0.0);
                        Gmap.addOverlay(obj.arrPolygonWiki[obj.posWiki]);               
                        obj.AddEvents_Wiki(obj.posWiki);
                        obj.posWiki++;
                    }
                }
            };
            request.send();
        }
    }
    obj.AddEvents_Wiki=function(i)
    {     
        var mousemovewiki = GEvent.addListener(obj.arrPolygonWiki[i], 'mousemove', function(){ 
            var opts = {color: '#fecd0e', weight: 1, opacity: 0.2};
            obj.arrPolygonWiki[i].setFillStyle(opts);
            opts = {color: '#fecd0e', weight: 1, opacity: 1};
            obj.arrPolygonWiki[i].setStrokeStyle(opts);
            $(".tooltipmap").css("visibility","hidden");
            sTooltip(obj.arrPolygonWiki[i],obj.aname[i]);       
        });
        var mouseoutwiki = GEvent.addListener(obj.arrPolygonWiki[i], 'mouseout', function(){
            var opts = {color: '#FF0000', weight: 1, opacity: 0.0};
            obj.arrPolygonWiki[i].setFillStyle(opts);
            opts = {color: '#FFFFFF', weight: 1, opacity: 1};
            obj.arrPolygonWiki[i].setStrokeStyle(opts);        
            $(".tooltipmap").css("visibility","hidden");               
        });
    }
    
    // end region wiki
    //********************************
    return obj;    
}

//phuong add wiki




//End demo cross domain    

 /*    
    if(window.XDomainRequest)
    {     
       xmlhttp = new XDomainRequest();
       xmlhttp.onload = function() 
       {
          //alert(xmlhttp.responseText);      
           var json = xmlhttp.responseText;
            if(typeof(json) != 'object')
              {  json = $.parseJSON(json);
              }               
            json = json.folder;          
            var i = 0;
            var j = 0;
            for(i = 0; i < json.length; i++)
            {// FakeWiki
                // Kiem tra ton tai                       
                if(id.indexOf(json[i].id) == -1)
                {
                    id[pos] = json[i].id;
                    aname[pos] = json[i].name;
                    // Get location
                    var poly_points = json[i].polygon;
                    var points = new Array();
                    for(j = 0; j < poly_points.length; j++)
                    {
                        points[j] = new GLatLng(poly_points[j].y, poly_points[j].x);
                    }
                    polygon[pos] = new GPolygon(points, '#FFFFFF', 1, 1, '#FF0000', 0.0);
                    Gmap.addOverlay(polygon[pos]);               
                    AddEvents_Wiki(pos);
                    pos++;
                }
            }
        }         
       xmlhttp.open("GET",st);
       xmlhttp.send(""); 
}else{ 
    
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }  
    xmlhttp.onreadystatechange=function()
    {
       //alert(xmlhttp.responseText);
      
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {         
        var json = xmlhttp.responseText;    
      
            if(typeof(json) != 'object')
              {  json = $.parseJSON(json);
              }               
            json = json.folder;          
            var i = 0;
            var j = 0;
            for(i = 0; i < json.length; i++)
            {// FakeWiki
                // Kiem tra ton tai                       
                if(id.indexOf(json[i].id) == -1)
                {
                    id[pos] = json[i].id;
                    aname[pos] = json[i].name;
                    // Get location
                    var poly_points = json[i].polygon;
                    var points = new Array();
                    for(j = 0; j < poly_points.length; j++)
                    {
                        points[j] = new GLatLng(poly_points[j].y, poly_points[j].x);
                    }
                    polygon[pos] = new GPolygon(points, '#FFFFFF', 1, 1, '#FF0000', 0.0);
                    Gmap.addOverlay(polygon[pos]);               
                    AddEvents_Wiki(pos);
                    pos++;
                }
            }
        
        }
      }
      xmlhttp.open("GET",st,true);
      xmlhttp.send(null);
    }
*/  

function sTooltip(polygon,aname,t)
{          
    
    var mousex = document.getElementById("toadoX").value;//1020
    var mousey = document.getElementById("toadoY").value;
    var onresize = document.getElementById("mapdivonresize").value;
    if(onresize == 0)
    {          
        mousex-=355;//650
        mousey-=35;
       // alert(mousey);
        if(mousex >= 550)  mousex-=100;
        if(mousey >= 550) mousey-=40;
        if(mousey <= 0) mousey+=40;
    }           
    var mapdiv = document.getElementById("maptracking");
    var tooltip = document.createElement("div");
    tooltip.id="tooltipmap";
    mapdiv.appendChild(tooltip);        
    polygon.tooltip = '<div class="tooltipmap"><table><tr><td>'+aname+'</td></tr></table></div>';
    tooltip.innerHTML = polygon.tooltip ;
    $(".tooltipmap").css("left",mousex.toString()+"px");
    $(".tooltipmap").css("top",mousey.toString()+"px");               
    tooltip.style.visibility ='visible';       
}


//end phuong add 
function CustomGetTileUrl(a,b)
{//alert("http://map2.vngeobiz.com/tilemap/tilemapservice?lvl=" + b + "&top="+ a.y + "&left=" + a.x + "&type=vector");
    return "http://map2.vngeobiz.com/tilemap/tilemapservice?lvl=" + b + "&top="+ a.y + "&left=" + a.x + "&type=vector";
    //return "file:///C:/Documents and Settings/T61P/Desktop/Google/Google/Z" + b + "/" + a.y + "_" + a.x + ".png"; // replace that with a "real" URL
}
